import editorProjectConfig from '../../pages/power-editor/data-model/data-model'
import { createUUID } from '@/common/js/mUtils'
import { cloneDeep, debounce } from 'lodash'
//假设一个项目对应一个页面
const state = {
	projectDataInfo: editorProjectConfig.getProjectConfig(),
	activeElementUUID: '',
	gridNum: 10
}

const actions = {
	// setProjectDataInfo:debounce(({ commit }, data)=>{
	// 	commit('setProjectDataInfo', data)
	// },200),
	setProjectDataInfo({ commit }, data) {
		commit('setProjectDataInfo', data)
	},
	setActiveElementUUID({ commit }, data) {
		commit('setActiveElementUUID', data)
	},
	addElement({ commit }, data) {
		// debugger
		commit('addElement', data)
	},
	removeElement({ commit }, data) {
		commit('removeElement', data)
	},
	moveSelectedTag({ commit }, data) {
		commit('moveSelectedTag', data)
	}
}

const mutations = {
	setGridNum(state, data) {
		state.gridNum = data
	},
	setProjectDataInfo(state, data) {
		state.projectDataInfo = data
	},
	setActiveElementUUID(state, data) {
		state.activeElementUUID = data
	},
	addElement(state, data) {
		let copy = cloneDeep(data)
		let left = copy.props[0].fields[1].value[0].value.value
		let top = copy.props[0].fields[1].value[1].value.value
		let zindex = copy.props[0].fields[1].value[2].value.value
		copy.uuid = createUUID()
		copy.props[0].fields[1].value[0].value.value = left + 10
		copy.props[0].fields[1].value[1].value.value = top + 10
		copy.props[0].fields[1].value[2].value.value = zindex + 1
		state.projectDataInfo.pages[0].elements.push(copy)
		this.commit('setActiveElementUUID', copy.uuid)
	},
	removeElement(state, data) {
		let activeElementIndex = state.projectDataInfo.pages[0].elements.findIndex(v => {
			return v.uuid === data
		})
		if (activeElementIndex >= 0) {
			state.projectDataInfo.pages[0].elements.splice(activeElementIndex, 1)
			state.activeElementUUID = ''
		}
	},
	moveSelectedTag(state, { uuid, num }) {
		let activeElement = state.projectDataInfo.pages[0].elements.find(v => {
			return v.uuid === uuid
		})
		let zindex = activeElement.props[0].fields[1].value[2].value.value
		if (num && num > -2 && num < 2) {
			activeElement.props[0].fields[1].value[2].value.value = zindex + num
		} else {
			activeElement.props[0].fields[1].value[2].value.value = num
		}
	}
}

const getters = {
	projectDataInfo(state) {
		return state.projectDataInfo
	},
	activeElementUUID(state) {
		return state.activeElementUUID
	},
	activeElement(state) {
		return state.projectDataInfo.pages[0].elements.find(element => {
			return element.uuid === state.activeElementUUID
		})
	},
	activePage(state) {
		return state.projectDataInfo.pages[0]
	},
	activeElementIndex(state) {
		return state.projectDataInfo.pages[0].elements.findIndex(v => {
			return v.uuid === state.activeElementUUID
		})
	}
}

export default {
	state,
	getters,
	actions,
	mutations
}
